import 'package:flutter/material.dart';
import 'package:shopping_group/modle/main/bloc/notice_bloc.dart';
import 'package:shopping_group/modle/main/entity/report_entity.dart';
import 'package:shopping_group/res/colors.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:shopping_group/res/dimens.dart';
import 'package:shopping_group/utils/object_util.dart';
import 'package:shopping_group/utils/widget_utils.dart';

class NoticePage extends StatefulWidget {
  @override
  _NoticePageState createState() => _NoticePageState();
}

class _NoticePageState extends State<NoticePage> {
  List<String> _itmes = [];
  NoticeBloc _bloc;
  ReportEntity _reportEntity = ReportEntity();
  @override
  void initState() {
    super.initState();
    _bloc = NoticeBloc();
    _bloc.initDate.add(null);
    _itmes
      ..add(
          'https://lizhengyouxuan.oss-cn-shanghai.aliyuncs.com/lizhengyouxuan/other/notice1.png');
  }

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
        onTap: (() {
          FocusScope.of(context).requestFocus(FocusNode());
        }),
        child: Scaffold(
          resizeToAvoidBottomInset: false,
          body: Container(
            height: double.infinity,
            decoration: new BoxDecoration(
              color: Colours.gray_f0,
            ),
            child: _groupView(),
          ),
        ));
  }

  Widget _groupView() {
    return StreamBuilder(
      stream: _bloc.sendData,
      initialData: _reportEntity,
      builder: (BuildContext contexts, AsyncSnapshot<Object> snapshot) {
        Object object = snapshot.data;
        if (object is ReportEntity) {
          _reportEntity = object;
        }
        return Column(
          children: <Widget>[
            _swiperView(),
            SizedBox(
              height: 8,
            ),
            _titleView(),
            SizedBox(
              height: 8,
            ),
            Expanded(child: _contentView()),
            SizedBox(
              height: 8,
            ),
            _timeView(),
            SizedBox(
              height: 8,
            ),
          ],
        );
      },
    );
  }

  Widget _timeView() {
    return Container(
      alignment: Alignment.centerRight,
      margin: EdgeInsets.symmetric(horizontal: Dimens.FONT_SIZE_8),
      padding: EdgeInsets.symmetric(
          vertical: Dimens.FONT_SIZE_12, horizontal: Dimens.FONT_SIZE_16),
      decoration: BoxDecoration(
          // border: Border.all(color: Colours.title_white, width: 0.0),
          // borderRadius: BorderRadius.circular(10.0),
          //  color: Colours.title_white
          ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          WidgetUtils.getTextColorView(
            ObjectUtil.isEmptyString(_reportEntity.deployer)
                ? ''
                : '发布人：${_reportEntity.deployer}',
            Colours.text_2222,
            Dimens.FONT_SIZE_16,
            textAlign: TextAlign.center,
          ),
          SizedBox(
            height: 8,
          ),
          WidgetUtils.getTextColorView(
            ObjectUtil.isEmptyString(_reportEntity.deployDate)
                ? ''
                : _reportEntity.deployDate,
            Colours.text_2222,
            Dimens.FONT_SIZE_16,
            textAlign: TextAlign.center,
          ),
        ],
      ),
    );
  }

  Widget _titleView() {
    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.symmetric(horizontal: Dimens.FONT_SIZE_8),
      padding: EdgeInsets.symmetric(vertical: Dimens.FONT_SIZE_12),
      decoration: BoxDecoration(
          border: Border.all(color: Colours.title_white, width: 0.0),
          borderRadius: BorderRadius.circular(10.0),
          color: Colours.title_white),
      child: WidgetUtils.getTextColorView(
          ObjectUtil.isEmptyString(_reportEntity.title)
              ? ''
              : _reportEntity.title,
          Colours.text_2222,
          Dimens.FONT_SIZE_18,
          textAlign: TextAlign.center,
          fontweight: FontWeight.w500),
    );
  }

  Widget _contentView() {
    return Container(
      alignment: Alignment.centerLeft,
      height: MediaQuery.of(context).size.height / 4,
      margin: EdgeInsets.symmetric(horizontal: Dimens.FONT_SIZE_8),
      padding: EdgeInsets.symmetric(
          vertical: Dimens.FONT_SIZE_12, horizontal: Dimens.FONT_SIZE_16),
      decoration: BoxDecoration(
          border: Border.all(color: Colours.title_white, width: 0.0),
          borderRadius: BorderRadius.circular(10.0),
          color: Colours.title_white),
      child: SingleChildScrollView(
        scrollDirection: Axis.vertical,
        reverse: false,
        padding: EdgeInsets.all(0.0),
        physics: BouncingScrollPhysics(),
        child: WidgetUtils.getTextColorView(
          ObjectUtil.isEmptyString(_reportEntity.detail)
              ? ''
              : _reportEntity.detail,
          Colours.text_2222,
          Dimens.FONT_SIZE_16,
          // textAlign: TextAlign.center,
        ),
      ),
    );
  }

  Widget _swiperView() {
    return Container(
        decoration: BoxDecoration(
            border: Border.all(color: Colours.gray_f0, width: 0),
            borderRadius: BorderRadius.circular(15.0),
            color: Colours.gray_f0),
        // padding: const EdgeInsets.only(top: 16, left: 16, right: 16),
        width: MediaQuery.of(context).size.width,
        height: 200.0,
        child: Swiper(
          itemBuilder: _swiperBuilderOne,
          itemCount: _itmes.length,
          // pagination: SwiperPagination(
          //     builder: DotSwiperPaginationBuilder(
          //   color: Colors.transparent,
          //   activeColor: Colours.transparent,
          // )),
          //  control: SwiperControl(),
          scrollDirection: Axis.horizontal,
          autoplay: false,
          onTap: (index) {},
        ));
  }

  Widget _swiperBuilderOne(BuildContext context, int index) {
    return FadeInImage.assetNetwork(
      image: _itmes[index],
      placeholder: 'images/picture_loading.png',
      fit: BoxFit.fill,
    );
  }
}
